<?php
declare(strict_types=1);

namespace app\platform\business;

use app\platform\model\Enterprise as EnterpriseModel;
use think\facade\Log;

class Enterprise extends PlatformBase
{
    private $_enterpriseObj = null;

    public function __construct()
    {
        $this->_enterpriseObj = new EnterpriseModel();
    }

    public function getAllEnterprise(array $where = []): array
    {
        $rows = null;
        try {
            $rows = $this->_enterpriseObj->getAllEnterprise($where);
        } catch (\Exception $e) {
            logErr('读取所有企业失败', sprintf('method:%s,err:%s', __METHOD__, $e->getMessage()));
        }

        return $rows ? $rows->toArray() : [];
    }

    public function getEnterpriseList(int $page = 1, int $limit = 10, array $where = []): array
    {
        $rows  = [];
        $count = 0;
        try {
            $rows  = $this->_enterpriseObj->getEnterpriseList($page, $limit, $where);
            $count = $this->_enterpriseObj->getEnterpriseCount($where);
        } catch (\Exception $e) {
            logErr('读取企业列表失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return ['rows' => $rows ? $rows->toArray() : [], 'count' => $count];
    }

    public function addEnterprise(array $data): array
    {
        try {
            return [self::_RESULT => $this->_enterpriseObj->addEnterprise($data), self::_MESSAGE => self::_ADD_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('添加企业失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function findEnterpriseById(int $id): array
    {
        $enterprise = null;
        try {
            $enterprise = $this->_enterpriseObj->findEnterpriseById($id);
        } catch (\Exception $e) {
            logErr('通过主键读取企业详细信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        if (empty($enterprise)) {
            return [];
        }

        return $enterprise->toArray();
    }

    public function updateEnterprise(int $id, array $data): array
    {
        try {
            return [self::_RESULT => $this->_enterpriseObj->updateEnterprise($id, $data), self::_MESSAGE => self::_ADD_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键更新企业信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }

    public function deleteEnterprise(int $id): array
    {
        try {
            return [self::_RESULT => $this->_enterpriseObj->deleteEnterprise($id), self::_MESSAGE => self::_DELETE_SUCCESS_MESSAGE];
        } catch (\Exception $e) {
            logErr('通过主键删除企业信息失败', sprintf('websiteCode:%s,method:%s,err:%s', $this->_websiteCode, __METHOD__, $e->getMessage()));
        }

        return [self::_RESULT => false, self::_MESSAGE => self::_SYSTEM_ERROR_MESSAGE];
    }
}
